#include<bits/stdc++.h>
#include<opencv2/opencv.hpp>

using namespace cv;
using namespace std;

uchar to16(char x){
    if (isdigit(x))
        return x - '0';
    if (islower(x))
        return x - 'a' + 10;
    if (isupper(x))
        return x - 'A' + 10;
    return 0;
}

/**
 * @brief 对图像低3个位平面进行异或操作
 * 
 * @param img 输入图像
 * @param key 密钥 由SHA-256对输入数字串产生
 * 
 * @return 结果图像
*/
Mat solve(Mat img, char *key){
    int rows = img.rows;
	int cols = img.cols;
    int len = strlen(key);
    char *ch = key;
    for (int i=0; i<rows;i++){
	    Vec4b *p = img.ptr<Vec4b>(i);
	    for (int j=0;j<cols;j++){
		    Vec4b &pix = *p++;
            uchar mask = to16(*ch);
            pix[0] ^= mask & 0x07;
            pix[1] ^= (mask & 0x0e) >> 1;
            pix[2] ^= mask & 0x07;
            if (++ch >= key+len)
                ch = key;
	    }
    }
    return img;
}